class KeywordModel {
  key = 'searchHistory';
  maxLength = 10;

  getHistoryWords() {
    const words = my.getStorageSync({ key: this.key });
    if (!words.data) return [];
    return words.data;
  }

  addToHistoryWords(keyword) {
    let words = this.getHistoryWords();
    const exist = words.includes(keyword);

    if (!exist) {
      if (words.length >= this.maxLength) {
        words.pop();
      }
      words.unshift(keyword);
      my.setStorageSync({
        key: this.key,
        data: words,
      });
    }
  }
}

export function createKeywordModel() {
  return new KeywordModel();
}
